//
//  LGCancerSymptomDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/22.
//  癌症 -- 症状详情

#import "LGCancerSymptomDetailController.h"
#import "LGCancerSymptomDetailCell.h"
#import "LGCanceSymptomEmptyCell.h"
@interface LGCancerSymptomDetailController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *headView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGCancerSymptomDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"症状详情";
    [self.headView addSubview:self.titleLabel];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/symptomListDetails" parameters:@{@"patient_id":self.patientid,@"config_id":self.configid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.titleLabel.text = LGNSString(returnData[@"symptom_name"]);
            NSArray *itemArray = returnData[@"treat_types"];
            CGFloat top = viewPix(45);
            CGFloat left = viewPix(18);
            CGFloat maxWidth = Screen_W-viewPix(36);
            for (NSString *content in itemArray) {
                CGFloat contentW = [content textWidthWithFont:LGFont(12) maxHeight:MAXFLOAT]+viewPix(16);
                top = (left+contentW)>maxWidth?top+viewPix(24):top;
                left = (left+contentW)>maxWidth?viewPix(18):left;
                UILabel *label = [UILabel lableWithText:content colorString:@"#5E8EF5" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
                label.frame = CGRectMake(left, top, contentW, viewPix(16));
                label.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
                label.cornerRidus = viewPix(8);
                [self.headView addSubview:label];
                left = kMaxX(label.frame)+viewPix(8);
            }
            top = (itemArray.count>0)?top+viewPix(20):viewPix(40);
            self.headView.frame = CGRectMake(0, 0, Screen_W, top);
            self.tableView.tableHeaderView = self.headView;
            self.dataArray = returnData[@"list"];
            [self.tableView reloadData];
        }
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无报告" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无报告" offset:0];
        }
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"list"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.section][@"list"][indexPath.row];
    if ([tempDic[@"content"] count] == 0 && [tempDic[@"images"] count] == 0) {
        //空白页
        LGCanceSymptomEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emptyCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LGCancerSymptomDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataDic = tempDic;
        return cell;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    UIView *iconView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(19), viewPix(20), 4, viewPix(14))];
    iconView.backgroundColor = [UIColor colorWithString:@"#77D4C6"];
    iconView.cornerRidus = 2;
    [headView addSubview:iconView];
    UILabel *titleLabel = [UILabel lableWithText:LGNSString(self.dataArray[section][@"record_date"]) colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    [headView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(viewPix(8));
        make.centerY.equalTo(iconView);
    }];

    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGCancerSymptomDetailCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[LGCanceSymptomEmptyCell class] forCellReuseIdentifier:@"emptyCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(18), viewPix(12), Screen_W-viewPix(30), viewPix(25));
    }
    return _titleLabel;
}

-(UIView *)headView{
    if(!_headView){
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    }
    return _headView;
}

@end
